package com.heatup.apt.management.service.coupon.impl;

import com.heatup.apt.common.dao.ActivityInfoMapper;
import com.heatup.apt.common.dao.ActivityUserMapper;
import com.heatup.apt.management.service.coupon.ActivityService;
import com.heatup.apt.model.coupon.ActivityInfo;
import com.heatup.apt.model.coupon.ActivityUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ActivityServiceImpl  implements ActivityService {
	private static final Logger logger = LoggerFactory.getLogger(ActivityServiceImpl.class);
	
	@Autowired
	private ActivityInfoMapper activityInfoMapper;
	
	@Autowired
	private ActivityUserMapper activityUserMapper;
	
	/**
	 * 当id不存在的时候就插入
	 * id存在的时候就更新
	 * 
	 * 
	 */
	@Override
	public int saveActivity(ActivityInfo activityInfo) {
		logger.info("===================================save activity start======================");
		int bool = 0 ;
		// 1 有id 修改
		if(activityInfo.getId() != null){
			logger.info("===================================update activity ======================");
			bool = activityInfoMapper.updateSelectiveById(activityInfo);
			
		}else{
			logger.info("===================================insert activity ======================");
			activityInfo.setCreateTime(new Date());
			bool = activityInfoMapper.insert(activityInfo);
		}
		logger.info("===================================save activity bool:"+bool+"======================");
		return bool;
	}
	
	/**
	 * 查询所有的活动
	 */
	@Override
	public Map<String,Object> queryActivityInfoList(Map<String,Object> mapwhere) {
		Map<String,Object> map = new HashMap<>();
		int pageCount = 10 ;
		int pageSum = 0;
		int pageNum = (Integer)mapwhere.get("pageNum");
		map.put("pageNum",pageNum);
		if( pageNum != 0){
			pageNum = pageCount * (pageNum-1);
			mapwhere.put("pageNum", pageNum);
			mapwhere.put("pageCount", pageCount);
		}else{
			mapwhere.put("pageCount", pageCount);
		}
		
		List<ActivityInfo> list = activityInfoMapper.queryActivityInfoList(mapwhere);
		mapwhere.remove("pageNum");
		List<ActivityInfo> listAll = activityInfoMapper.queryActivityInfoList(mapwhere);
		map.put("list", list);
		map.put("pageSum", listAll.size());
		map.put("activityName",mapwhere.get("activityName"));
		map.put("inviteCouponId",mapwhere.get("inviteCouponId"));
		map.put("couponId",mapwhere.get("couponId"));
		return map;
	}
	
	/**
	 * 活动编辑
	 * 当活动开始的时候就不能编辑
	 * 活动未开始的时候是可以编辑
	 */
	@Override
	public Map<String, Object> activityEdit(int activityId) {
		Map<String,Object> mapWhere = new HashMap<>();
		Map<String,Object> mapresponse = new HashMap<>();
		mapWhere.put("activityId", activityId);
		ActivityInfo activityInfo = activityInfoMapper.selectById(activityId);
		List<ActivityUser> listAU = activityUserMapper.queryActivityUser(mapWhere);
		mapresponse.put("isEdit", listAU.size());
		mapresponse.put("activityInfo",activityInfo);
		return mapresponse;
	}
	
}
